import type { Ref } from 'vue'

/**
 * table 方法封装
 * @param el ref<table>实例
 * @param getDataSource 获取数据源方法
 * @returns
 */
export function useTableUtils<T extends any[]>(
  el: Ref<any>,
  getDataSource: RefSource<T>
) {
  const clearAll = () => {
    el.value?.clearSelection()
  }

  const dataSource = getSource(getDataSource)

  const selectAll = () => {
    dataSource.value.forEach(row => el.value?.toggleRowSelection(row, true))
  }

  const toggleAll = (val: boolean) => {
    val ? selectAll() : clearAll()
  }

  const getRows = (): T => {
    return el.value?.getSelectionRows()
  }

  return { clearAll, selectAll, toggleAll, getRows }
}
